package basic.courseLearn04.problem;

import org.junit.Test;

//打印链表公共部分
public class PublicPartLinkList {

    //单链表的节点
    private class Node<T> {
        private T data;
        private Node<T> next;

        public Node() {
        }

        public Node(T data, Node<T> next) {
            this.data = data;
            this.next = next;
        }
    }

    //打印公共部分
    public void printCommonPart(Node<Integer> head1,Node<Integer> head2 ){
        while(head1!=null && head2!=null){
            if(head1.data < head2.data){
                head1 = head1.next;
            }else if(head1.data > head2.data){
                head2 = head2.next;
            }else{
                System.out.print(head1.data+" ");
                head1 = head1.next;
                head2 = head2.next;
            }
        }
    }

    @Test
    public void test(){
        //创建两个链表
        Node head1 = new Node(1, new Node(3, new Node(5, new Node(7, new Node(9, null)))));
        Node head2 = new Node(2, new Node(3, new Node(4, new Node(5, null))));

        //打印公共部分
        printCommonPart(head1, head2);
    }







}
